﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.ComponentModel.Design;
using ProgNetComponents.Utils;
using System.Collections;
using System.Data;
using System.Windows.Forms;

namespace ProgNetComponents.Design.Editors
{
    /// <summary>
    /// 
    /// </summary>
    public class DataSetEditor : BaseListEditor
    {
        /// <summary>
        /// Gets a value indicating whether [always refresh items].
        /// </summary>
        /// <value>
        ///   <c>true</c> if [always refresh items]; otherwise, <c>false</c>.
        /// </value>
        protected override bool AlwaysRefreshItems
        {
            get
            {
                return true;
            }
        }

        /// <summary>
        /// Fills the collection.
        /// </summary>
        /// <param name="collection">The collection.</param>
        /// <param name="context">The context.</param>
        /// <param name="provider">The provider.</param>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        protected override bool FillCollection(Collections.NamedObjectCollection collection, System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            IContainer container = (IContainer)null;
            IDesignerHost hostSvc = provider.GetService<IDesignerHost>();

            if (context != null)
                container = context.Container;
            if (container == null && hostSvc != null)
                container = hostSvc.Container;
            if (container == null)
                return true;

            collection.Add("(none)", null);

            ComponentCollection components = DesignUtils.CheckForNestedContainer(container).Components;
            foreach (IComponent dataSource in (ReadOnlyCollectionBase)components)
            {
                DataSet ds = dataSource as DataSet;
                if (ds !=null)
                {
                    collection.Add(ds.DataSetName, ds);
                }
            }

            return true;
        }
    }
}
